package com.example.weddingapp.fragment;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.lifecycle.AndroidViewModel;


import com.example.weddingapp.R;
import com.example.weddingapp.activity.AboutActivity;
import com.example.weddingapp.activity.LoginActivity;
import com.example.weddingapp.activity.MyInfoActivity;
import com.example.weddingapp.activity.WalletActivity;
import com.example.weddingapp.base.BaseFragment;
import com.example.weddingapp.databinding.FragmentMeBinding;
import com.example.weddingapp.utils.ImageLoaderUtil;

import cn.leancloud.AVFile;
import cn.leancloud.AVUser;

public class MeFragment extends BaseFragment<AndroidViewModel, FragmentMeBinding> implements View.OnClickListener {


    @Override
    public int setContent() {
        return R.layout.fragment_me;
    }

    @Override
    public void onResume() {
        super.onResume();
        getData();
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        showContentView();
        bindingView.llInfo.setOnClickListener(this);
        bindingView.tvExit.setOnClickListener(this);
        bindingView.tvWallet.setOnClickListener(this);
        bindingView.tvAbout.setOnClickListener(this);
        getData();
    }

    private void getData() {
        //拿到当前用户
        AVUser currentUser = AVUser.getCurrentUser();
        if (currentUser == null) return;
        //用户名
        String username = currentUser.getUsername();
        String nick = currentUser.getString("nick");
        if (TextUtils.isEmpty(nick)) {
            bindingView.tvNick.setText(username);
        } else {
            bindingView.tvNick.setText(nick);
        }
        String sign = currentUser.getString("sign");
        if (!TextUtils.isEmpty(sign)) {
            bindingView.tvIntroduction.setText(sign);
        } else {
            bindingView.tvIntroduction.setText("点我设置签名");
        }

        AVFile headImg = currentUser.getAVFile("headImg");
        if (headImg != null)
            ImageLoaderUtil.displayHeadImage(getContext(), headImg.getUrl(), bindingView.ivHead);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_info:
                startActivity(new Intent(getContext(), MyInfoActivity.class));
                break;
            case R.id.tv_wallet:
                //钱包
                startActivity(new Intent(getContext(), WalletActivity.class));
                break;
            case R.id.tv_about:
                //关于我们
                startActivity(new Intent(getContext(), AboutActivity.class));
                break;
            case R.id.tv_exit:
                //退出登录
                logout();
                break;
        }
    }

    /**
     * 退出登录
     */
    private void logout() {
        AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
        builder
                .setTitle("退出登录")
                .setMessage("您是否要退出登录?")
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        AVUser.getCurrentUser().logOut();
                        dialogInterface.dismiss();
                        startActivity(new Intent(getContext(), LoginActivity.class));
                        getActivity().finish();
                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        dialogInterface.dismiss();
                    }
                })
                .create().show();
    }

}